package top.kaoshanji.p2ch09a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 代码清单-p2ch09a-300页a：使用类加载器来加载属性文件的示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/2 13:55
 */
public class LoadResource {

    private static final Logger logger = LogManager.getLogger(LoadResource.class);

    public static void main(String[] args) throws IOException {

        logger.info("...代码清单-p2ch09a-300页a：使用类加载器来加载属性文件的示例...");

        LoadResource lr = new LoadResource();
        Properties props = lr.loadConfig();
        logger.info("...属性名称mode的值：{}...", props.getProperty("mode"));

    }

    public Properties loadConfig() throws IOException {
        ClassLoader loader = this.getClass().getClassLoader();
        InputStream input = loader.getResourceAsStream("top/kaoshanji/p2ch09a/config.properties");
        if (input == null) {
            throw new IOException("找不到配置文件。");
        }

        Properties props = new Properties();
        props.load(input);
        return props;
    }


}
